package com.mark.mall.admin.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.mark.mall.admin.platform.dto.ODetailDTO;
import com.mark.mall.admin.platform.dto.OrderDTO;
import com.mark.mall.admin.platform.dto.OrderDetailDTO;
import com.mark.mall.admin.platform.dto.PageParamDTO;
import com.mark.mall.admin.platform.entity.Member;
import com.mark.mall.admin.platform.entity.Order;
import com.mark.mall.admin.platform.entity.OrderDetail;
import com.mark.mall.admin.platform.entity.Product;
import com.mark.mall.admin.platform.mapper.MemberMapper;
import com.mark.mall.admin.platform.mapper.ODetailMapper;
import com.mark.mall.admin.platform.mapper.OrderMapper;
import com.mark.mall.admin.platform.mapper.ProductMapper;
import com.mark.mall.admin.platform.service.OrderService;
import com.mark.mall.common.myException.MyException;
import com.mark.mall.common.resultBean.R;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 销售单表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-02-28
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    private final ODetailMapper oDetailMapper;
    private final MemberMapper memberMapper;
    private final ProductMapper productMapper;

    @Override
    public R<PageInfo<OrderDTO>> orderPageInfo(PageParamDTO param, String orderNo) {
        PageInfo<OrderDTO> pageInfo = PageMethod.startPage(param.getPageNo(), param.getPageSize())
                .doSelectPageInfo(() ->
                        list(Wrappers.lambdaQuery(BeanUtil.copyProperties(orderNo, Order.class))));
        return new R<>(pageInfo);
    }

    @Override
    public R<Boolean> delOrder(String id) {
//        Order order = baseMapper.selectById(id);
//        if (order == null) {
//            throw new MyException("id为{" + id + "}的销售单不存在！");
//        }
//        order.setState(PlatformConstant.STATE_NO);
//        baseMapper.update(order, new QueryWrapper<Order>().lambda().eq(Order::getId, id));
        baseMapper.deleteById(id);
        return new R<>(true, "删除成功！");
    }

    @Override
    public R<OrderDetailDTO> detail(String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            throw new MyException("参数缺失，请核实！");
        }
        Order order = baseMapper.selectById(orderId);
        if (order == null) {
            throw new MyException("id为{" + orderId + "}的销售单不存在！");
        }
        Member member = memberMapper.selectById(order.getMemberid());
        if (member == null) {
            throw new MyException("id为{" + order.getMemberid() + "}的会员不存在！");
        }
        List<ODetailDTO> list = new ArrayList<>();
        // 利用构造方法赋值
        OrderDetailDTO detailDTO = new OrderDetailDTO(order);
        detailDTO.setMakeName(!StringUtils.isEmpty(member.getName()) ? member.getName() : "");
        // 根据订单id获取，其下的订单详情
        List<OrderDetail> detailList = oDetailMapper.selectList(new QueryWrapper<OrderDetail>().lambda().eq(OrderDetail::getOrderid, order.getId()));
        if (detailList != null && detailList.size() > 0) {
            for (OrderDetail orderDetail : detailList) {
                ODetailDTO dto = new ODetailDTO(orderDetail);
                Product product = productMapper.selectById(orderDetail.getProductid());
                if (product == null) continue;
                dto.setProductName(product.getName());
                list.add(dto);
            }
        }
        detailDTO.setOrderDetailList(list);
        return new R<>(detailDTO);
    }
}
